package com.example.papercutting;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.view.View;

@SuppressLint("ViewConstructor")
public class UnfoldPaper extends View {
    //    private final float degrees = Data.degrees;//旋转角度
    private final float degrees = Data.sweepAngle * 2;//旋转角度
    private final int rotateNumber = (int) (360 / (Data.sweepAngle * 2));

    //结果图的中心位置
    private int centerX;
    private int centerY;

    private final float[] matrixValues = {-1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f};//关于y轴对称矩阵
    private Matrix mMatrix;
    private Bitmap mBitmap;

    public UnfoldPaper(Context context, Bitmap bitmap) {
        super(context);
        centerX = Data.canvasWidth / 2;
        centerY = Data.canvasHeight / 2;
        mMatrix = new Matrix();
        mBitmap = bitmap;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mMatrix.reset();
        //画出对称前的区域
        mMatrix.postTranslate(0, -200);
        for (int i = 0; i < rotateNumber; i++) {
            mMatrix.postRotate(degrees, centerX, centerY);
            canvas.drawBitmap(mBitmap, mMatrix, null);
        }

        //画出对称后的区域
        mMatrix.setValues(matrixValues);
        mMatrix.postTranslate(2 * centerX, -200);
        for (int i = 0; i < rotateNumber; i++) {
            mMatrix.postRotate(degrees, centerX, centerY);
            canvas.drawBitmap(mBitmap, mMatrix, null);
        }
    }
}